package tv.danmaku.gtv;

import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class GTVPlayerFilePreloader {

    private String fileUrl;
    private int preloadLimit = 1000000;//默认预加载1M
    private long _nativeHandler = 0L;

    public interface OnPreloadListener {
        void onPreloadFinished();
    }

    private OnPreloadListener mListener = null;
    private Timer mTimer = null;
    private void beginTimer() {

        if( mTimer != null ) {
            try {
                mTimer.cancel();
                mTimer = null;
            }
            catch (Exception ex) {

            }
        }
        Log.e("POP", "timer started ");
        // 初始化定时器
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                Log.e("POP", "timer running ");
                notifyTimer();
            }

        }, 1000, 1000);
    }
    private void notifyTimer() {

        if( _nativeHandler != 0L ) {

            int finished = GTVPlayerCacheHelper.getInstance().checkAsyncPreloadFinished(_nativeHandler);
            if( finished == 1 ) {
                if( mListener != null ) {
                    mListener.onPreloadFinished();
                    mListener = null;
                }
                this.endTimer();
            }
        }
        else {
            this.endTimer();
        }
    }
    private void endTimer() {

        Log.e("POP", "timer stopped ");
        if( mTimer != null ) {
            try {
                mTimer.cancel();
                mTimer = null;
            }
            catch (Exception ex) {

            }
        }
    }

    public GTVPlayerFilePreloader(String url) {

        this.fileUrl = url;
        this.mListener = null;
    }

    public GTVPlayerFilePreloader(String url, int limit) {

        this.fileUrl = url;
        this.preloadLimit = limit;
        this.mListener = null;
    }

    public GTVPlayerFilePreloader(String url, int limit, OnPreloadListener l) {

        this.fileUrl = url;
        this.preloadLimit = limit;
        this.mListener = l;
    }

    public boolean startAsync() {

        GTVPlayerCacheHelper.getInstance().start();

        _nativeHandler = GTVPlayerCacheHelper.getInstance().startAsyncPreload(fileUrl, preloadLimit);
        if( _nativeHandler == 0L )
            return false;

        this.beginTimer();

        return true;
    }

    public boolean checkFinished() {

        if( _nativeHandler == 0L ) {
            Log.e("GTV", "checkFinished null");
            return true;
        }

        return (GTVPlayerCacheHelper.getInstance().checkAsyncPreloadFinished(_nativeHandler) > 0);
    }

    public void cancel() {

        if( _nativeHandler == 0L ) {
            Log.e("GTV", "cancel null");
            return;
        }

        GTVPlayerCacheHelper.getInstance().cancelAsyncPreload(_nativeHandler);
    }

    public void destroy() {

        this.endTimer();

        if( _nativeHandler != 0L ) {

            final long del = _nativeHandler;
            _nativeHandler = 0L;

            Runnable r = new Runnable() {
                @Override
                public void run() {
                    int finished = 0;
                    int times = 100;
                    do {
                        finished = GTVPlayerCacheHelper.getInstance().checkAsyncPreloadFinished(del);
                        times --;
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } while(finished == 0 && times > 0 );
                    GTVPlayerCacheHelper.getInstance().destroyAsyncPreload(del);
                }
            };

            Thread t = new Thread(r);
            t.start();
        }
    }
}
